package cn.itcast.bos.web.action.base;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.CourierService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


@Controller
@Namespace("/")
@Actions
@ParentPackage("json-default")
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {

	private static final long serialVersionUID = 1L;

	private Courier courier = new Courier();
	
	@Override
	public Courier getModel() {
		return courier;
	}
	
	@Autowired
	private CourierService courierService;
	
	
	//添加快递员
	@Action(value="courier_save",results={@Result(name="success",
			type="redirect",location="./pages/base/courier.html")})
	public String courierAdd() {
		courierService.save(courier);
		return SUCCESS;
	}
	
	//设置分页参数
	private int page;
	private int rows;
	
	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	//无条件分页查询
	@Action(value="courier_query",results={@Result(name="success",type="json")})
	public String queryCourier() {
		//页面封装
		Pageable pageable = new PageRequest(page-1, rows);
		//复杂的条件查询
		Specification<Courier> specification = new Specification<Courier>() {

			//Root 用于属性字段,CriteriaQuery 简单查询,CriteriaBuilder 复杂查询
			@Override
			public Predicate toPredicate(Root<Courier> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				//创建集合
				List<Predicate> list = new ArrayList<Predicate>();
				//简单的单表查询
				if(StringUtils.isNotBlank(courier.getCourierNum())) {
					//使用工号查询
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class),
							courier.getCourierNum());
					list.add(p1);
				}
				if(StringUtils.isNotBlank(courier.getCompany())) {
					//使用公司模糊查询
					Predicate p2 = cb.equal(root.get("company").as(String.class),"%"+courier.getCompany()+"%");
					list.add(p2);
				}
				if(StringUtils.isNotBlank(courier.getType())) {
					//使用员工类型查询,等值查询
					Predicate p3 = cb.equal(root.get("type").as(String.class), courier.getType());
					list.add(p3);
				}
				//多表查询
				Join<Courier,Standard> standardJoin = root.join("standard", JoinType.INNER);
				if(courier.getStandard() != null && StringUtils.isNotBlank(courier.getStandard().getName()) ) {
					Predicate p4 = cb.equal(standardJoin.get("name").as(String.class), "%"+ courier.getStandard().getName()+"%");
					
					list.add(p4);
				}
				
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		//查询
		Page<Courier> couriers = courierService.findPageData(specification,pageable);
		//创建map
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", couriers.getTotalElements());
		map.put("rows", couriers.getContent());
		//存入值栈中
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	//属性驱动
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}

	//删除,作废快递员
	@Action(value="curious_delete",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String delete() {
		String[] str = ids.split(",");
		//调用service,批量作废
		courierService.delete(str);
		return SUCCESS;
	}

}
